<?php

namespace api\modules\v1\controllers;

use Yii;
use api\common\controllers\CommonController;
use api\modules\v1\models\Authentication;
use api\modules\v1\models\User;
use api\modules\v1\models\Activityapply;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use api\modules\v1\models\Code;
class ActivityController extends CommonController
{
    public $modelClass = 'api\modules\v1\models\Activity';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }
    /*
     * 获取活动列表
     */
    public function actionList($page,$pagesize){
        $model = new $this->modelClass();
        return $model->getList($page,$pagesize);
    }
    /*
     * 单个活动获取接口
     */
    public function actionOne($actid){
        $model = new $this->modelClass();
        $modelData = $model::findOne(['actid'=>$actid]);
        return $modelData->attributes;
    }
    /*
     * 活动报名
     */
    public function actionActapply(){
        $model = new Activityapply();
        $model->attributes = Yii::$app->request->post();
        $phone = $model::findOne(['phone'=>$model->phone]);
        if(!empty($phone->attributes)){
            throw new ServerErrorHttpException('您已经申请过报名，不可重复报名.');
        }
        $model->time = date('Y-m-d H:i:s');
        if($model->save()){
            return [
                "message"=>"报名成功!请等待审核",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
}